package cn.edu.zufe.mjt;

import java.util.ArrayList;
import java.util.List;

//TIP 要<b>运行</b>代码，请按 <shortcut actionId="Run"/> 或
// 点击装订区域中的 <icon src="AllIcons.Actions.Execute"/> 图标。
public class Main {
    public static void main(String[] args) throws InterruptedException {
        int[] count = new int[]{1000};
        List<Thread> threads = new ArrayList<>();

        MyLock lock = new MyLock();

        for (int i = 0; i < 100; i++) {
            threads.add(new Thread(() -> {
                    lock.lock();
                    for (int j = 0; j < 10; j++) {
                        count[0]--;
                    }
                    lock.unlock();
            }));
        }
        long start = System.currentTimeMillis();
        for (Thread thread : threads) {
            thread.start();
        }
        for (Thread thread : threads) {
            thread.join();
        }
        long end = System.currentTimeMillis();

        System.out.println("Time elapsed: " + (end - start) + " ms");
        System.out.println("count = " + count[0]);
    }
}